﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;

using IrrKlang;

namespace MuzičkiFeršterker
{
    /// <summary>
    /// Class used to handle music types played with irrKlang engine
    /// </summary>
    class Play_irrKlang : Play_
    {
        private uint irr_pauseTime;

        /// <summary>
        /// Constructor used to set new song
        /// </summary>
        /// <param name="inputSong">Song that will be loaded</param>
        public Play_irrKlang(Song inputSong)
            : base (inputSong)
        {

        }

        /// <summary>
        /// Obsolete method. Playing and pausing is now handled by PlayHandler
        /// </summary>
        [Obsolete("PlayHandler handles play/pause")]
        public override void PlayPause()
        {
            throw new Exception("Play_irrKlang.PlayPause() is obsolete.");
        }

        /// <summary>
        /// Play method to start irrKlang song
        /// </summary>
        public override void Play()
        {
            if (currentSong == null)
                return;

            paused = false;
            irrSong = irrEngine.Play2D(this.currentSong.Path);
            if (currentBalance != 0)
                irrSong.Pan = -1 * currentBalance / 100;
        }

        /// <summary>
        /// Pause method for irrKlang engine
        /// </summary>
        public override void Pause()
        {
            if (currentSong == null)
                return;
            if (paused)
                return;

            irr_pauseTime = irrSong.PlayPosition;
            irrSong.Stop();

            paused = true;
        }

        /// <summary>
        /// Resume method for irrKlang engine
        /// </summary>
        public override void Resume()
        {
            if (currentSong == null)
                return;
            this.Play();
            irrSong.PlayPosition = irr_pauseTime;
        }

        /// <summary>
        /// Stop method for irrKlang engine
        /// </summary>
        public override void Stop()
        {
            if (currentSong == null)
                return;

            irrSong.Stop();
            paused = false;
        }

        /// <summary>
        /// Returns time for irrKlang played song
        /// </summary>
        /// <returns>Time in ms</returns>
        public override int getPosition()
        {
            if (currentSong == null)
                return 0;
            if (paused)
                return (int)irr_pauseTime;
            return (int)irrSong.PlayPosition;
        }

        /// <summary>
        /// Sets the position of the song on irrKlang engine
        /// </summary>
        /// <param name="pos">time in ms</param>
        public override void setPosition(int pos)
        {
            if (currentSong == null)
                return;
            if (paused)
                irr_pauseTime = (uint)pos;
            irrSong.PlayPosition = (uint)pos;
        }

        /// <summary>
        /// Method that returns if song has finished playing
        /// </summary>
        /// <returns>TRUE if finished, FALSE if still playing</returns>
        public override bool isFinished()
        {
            return irrSong.Finished;
        }
    }
}
